package com.linkage.linkedlife.ui.activity

import android.os.Bundle
import android.view.MenuItem
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.clark.dingding.constants.ARouterPath
import com.google.android.material.bottomnavigation.BottomNavigationView
import com.gyf.immersionbar.ImmersionBar
import com.linkage.linkedlife.R
import com.linkage.linkedlife.adapter.FragmentAdapter
import com.linkage.linkedlife.base.BaseActivity
import com.linkage.linkedlife.base.BaseFragment
import com.linkage.linkedlife.databinding.ActivityMainBinding
import com.linkage.linkedlife.model.AgentMsg
import com.linkage.linkedlife.model.MessageEvent
import com.linkage.linkedlife.model.RefreshMessageEvent
import com.linkage.linkedlife.ui.fragment.*
import com.linkage.linkedlife.ui.view.NoScrollViewPager
import okhttp3.internal.notify
import org.greenrobot.eventbus.EventBus
import java.util.*

/**
 * @ClassName: MainActivity
 * @Description: java类作用描述
 * @Author: Clark
 * @Date: 2021/1/12 0012 21:24
 */
@Route(path = ARouterPath.MainActivity)
class MainActivity : BaseActivity() {

    var mainBinding: ActivityMainBinding? = null
    private var mPager: NoScrollViewPager? = null
    private val mFragments: MutableList<BaseFragment> = ArrayList<BaseFragment>()
    private var mAdapter: FragmentAdapter? = null
    private var clickTime = 0L
    private var clickTabId = 0;


    var onNavigationItemSelectedListener =
        BottomNavigationView.OnNavigationItemSelectedListener { item ->
            resetToDefaultIcon()
            val i = item.itemId
            //点击的是同一个tab 并且间隔时间在500ms
            if (System.currentTimeMillis() - clickTime < 200 && clickTabId == i) {
                EventBus.getDefault()
                    .post(RefreshMessageEvent("refresh", "refreshTab" + mPager!!.currentItem));
            }
            clickTime = System.currentTimeMillis()
            clickTabId = item.itemId
            if (i == R.id.navigation_home) {
                mPager!!.currentItem = 0
                item.icon=getDrawable(R.mipmap.main_select)
                return@OnNavigationItemSelectedListener true
            } else if (i == R.id.navigation_eshop) {
                mPager!!.currentItem = 1
                item.icon=getDrawable(R.mipmap.eshop_select)
                return@OnNavigationItemSelectedListener true
            } else if (i == R.id.navigation_estore) {
                mPager!!.currentItem = 2
                item.icon=getDrawable(R.mipmap.estore_select)
                return@OnNavigationItemSelectedListener true
            } else if (i == R.id.navigation_mine) {
                mPager!!.currentItem = 3
                item.icon=getDrawable(R.mipmap.mine_select)
                return@OnNavigationItemSelectedListener true
            }
            false
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        ImmersionBar.with(this)
            .transparentStatusBar()  //透明状态栏，不写默认透明色
            .fullScreen(true)
            .statusBarColor(R.color.main_color)     //状态栏颜色，不写默认透明色
            .init();
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        mainBinding?.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
        mainBinding?.navigation?.itemIconTintList = null
        mPager = mainBinding?.containerPager
        mPager?.setScanScroll(false)
        mPager!!.offscreenPageLimit = 4
        val fragmentMain: AgentWebFragmentMain =
            ARouter.getInstance().build(ARouterPath.AgentWebFragmentMain)
                .navigation() as AgentWebFragmentMain
        val fragmentLife: AgentWebFragmentEshop =
            ARouter.getInstance().build(ARouterPath.AgentWebFragmentEshop)
                .navigation() as AgentWebFragmentEshop
        val fragmentCart: AgentWebFragmentPurchase =
            ARouter.getInstance().build(ARouterPath.AgentWebFragmentPurchase)
                .navigation() as AgentWebFragmentPurchase
        val fragmentMe: AgentWebFragmentUserCenter =
            ARouter.getInstance().build(ARouterPath.AgentWebFragmentUserCenter)
                .navigation() as AgentWebFragmentUserCenter
        val sendBundle: Bundle = Bundle()
        var agentMsgMain: AgentMsg = AgentMsg();
        var agentMsgAll: AgentMsg = AgentMsg();
        var agentMsgPurchase: AgentMsg = AgentMsg();
        var agentMsgUsr: AgentMsg = AgentMsg();
        agentMsgMain.url = "https://m-lcpro.lianchuanghj.com/?channelType=10000&hideTab=true";
        sendBundle.putSerializable("data", agentMsgMain)
        fragmentMain.setArguments(sendBundle)
        agentMsgAll.url = "https://m-lcpro.lianchuanghj.com/?channelType=0&hideTab=true";
        sendBundle.putSerializable("data", agentMsgAll)
        fragmentLife.setArguments(sendBundle)
        agentMsgPurchase.url =
            "https://m-lcpro.lianchuanghj.com/pages/shop-cart/index?channelType=10000&hideTab=true";
        sendBundle.putSerializable("data", agentMsgPurchase)
        fragmentCart.setArguments(sendBundle)
        agentMsgUsr.url =
            "https://m-lcpro.lianchuanghj.com/pages/user-center/index?channelType=10000&hideTab=true"
        sendBundle.putSerializable("data", agentMsgUsr)
        fragmentMe.setArguments(sendBundle)


        mFragments.add(fragmentMain)
        mFragments.add(fragmentLife)
        mFragments.add(fragmentCart)
        mFragments.add(fragmentMe)
        mAdapter = FragmentAdapter(supportFragmentManager, mFragments)
        mainBinding?.setViewPaAdapter(mAdapter)
    }
     fun  resetToDefaultIcon() {
         var home =  mainBinding?.navigation?.menu?.findItem(R.id.navigation_home)
         var eshop =  mainBinding?.navigation?.menu?.findItem(R.id.navigation_eshop)
         var estore =  mainBinding?.navigation?.menu?.findItem(R.id.navigation_estore)
         var mine =  mainBinding?.navigation?.menu?.findItem(R.id.navigation_mine)

         home?.icon=getDrawable(R.mipmap.main_normal)
         eshop?.icon=getDrawable(R.mipmap.eshop_normal)
         estore?.icon=getDrawable(R.mipmap.estore_normal)
         mine?.icon=getDrawable(R.mipmap.mine_normal)



     }

}